package com.hui.dao;


import com.hui.entity.Menu;
import com.hui.entity.Role;
import com.hui.mapper.MenuMapper;
import com.hui.mapper.RoleMapper;
import com.hui.util.DruidUtil;

import java.util.List;

/**
 * @Description Role持久层
 */
public class RoleDao {

    /**
     * 查询所有职位
     **/
    public List<Role> findAll() {

        String sql = "select * from tb_roles";
        return DruidUtil.commonFindListByParam(sql, new RoleMapper());

    }

    /**
     * 根据id删除职位
     **/
    public void delByRoleId(int roleId) {
        String sql = " delete from tb_roles where role_id= ? ";
        DruidUtil.commonUpdate(sql, roleId);
    }

    /**
     * 根据id查看职位数据
     **/
    public Role findByRoleId(int roleId) {
        String sql = "select *  from tb_roles where role_id=" + roleId;
        return DruidUtil.commonFindObjectByParam(sql, new RoleMapper());
    }

    /**
     * 添加职位,返回主键id
     **/
    public Integer addRole(Role role) {
        String sql = "insert into tb_roles values(null,?,?)";
        return DruidUtil.commonUpdateReturnKey(sql, role.getRoleName(), role.getRoleDesc());
    }

    /**
     * 添加职位-菜单关联表数据
     **/
    public void addRoleMenu(int roleId, int menuId) {
        String sql = "insert into tb_role_menu values(?,?)";
        DruidUtil.commonUpdateReturnKey(sql, roleId, menuId);
    }

    /**
     * 根据职位id来查询菜单数据
     **/
    public List<Menu> findMenuByRoleId(int roleId) {
        String sql = " select  me.* from  tb_role_menu em, tb_menus me\n" +
                "     where em.menu_id=me.menu_id \n" +
                "      and em.role_id=" + roleId;
        return DruidUtil.commonFindListByParam(sql, new MenuMapper());
    }

    /**
     * 根据id修改职位信息
     **/
    public void updateRole(Role role) {
        String sql = "update tb_roles set  role_name=?, role_desc=?   where role_id=?";
        DruidUtil.commonUpdate(sql, role.getRoleName(), role.getRoleDesc(), role.getRoleId());
    }

    /**
     * 根据职位id删除职位-菜单关联表数据
     **/
    public void deleteRoleMenuByRoleId(int roleId) {
        String sql = "delete from tb_role_menu where role_id=?";
        DruidUtil.commonUpdate(sql, roleId);
    }
}
